package com.wzdigit.wms.wms.controller.basic;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.common.utils.ValidatorUtil;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.basic.CreateLotAttrRequest;
import com.wzdigit.wms.basic.client.request.basic.SearchLotAttrRequest;
import com.wzdigit.wms.basic.client.request.basic.UpdateLotAttrRequest;
import com.wzdigit.wms.basic.client.request.basic.UpdateStatusLotAttrRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.basic.LotAttrResponse;
import com.wzdigit.wms.wms.constant.WmsWareConstant;
import com.wzdigit.wms.wms.service.basic.LotAttrService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 批次属性 前端控制器
 * @author yangdongjie
 * @LotAttrController.java
 * @2020年12月31日下午5:56:18
 */
@RestController
@Api(tags = "批次属性")
@RequestMapping(value = WmsWareConstant.API_VERSION)
public class LotAttrController {

	@Autowired
    private LotAttrService lotAttrService;

    private static final String REQUEST_MODEL = "/lotattr";
    
    /**
     * 查询批次属性列表
     * @param <T>
     * @param dto
     * @param servletRequest
     * @param servletResponse
     * @return
     */
	@SuppressWarnings("rawtypes")
	@ApiOperation(value = "查询批次属性列表",notes = "查询批次属性列表")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/searchLotAttr")
    public PagerInfo<List<LotAttrResponse>> searchLotAttr(
    		SearchLotAttrRequest dto, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return lotAttrService.searchLotAttr(dto);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    
    /**
     * 查询批次属性
     * @param locId
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "查询批次属性",notes = "查询批次属性")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/getLotAttr")
    public LotAttrResponse getLotAttr(
    		@ApiParam(value = "批次属性ID", required = true) 
    		@RequestParam Integer lotAttrId, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return lotAttrService.getLotAttr(lotAttrId);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    
    /**
     * 创建批次属性
     * @param dto
     * @param bindingResult
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "创建批次属性",notes = "创建批次属性")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PostMapping(value = REQUEST_MODEL + "/createLotAttr")
    public LotAttrResponse createLotAttr(
    		@RequestBody @Valid CreateLotAttrRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
        	ValidatorUtil.validator(bindingResult);
            return lotAttrService.createLotAttr(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    /**
     * 修改批次属性
     * @param dto
     * @param bindingResult
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "修改批次属性",notes = "修改批次属性")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PutMapping(value = REQUEST_MODEL + "/updateLotAttr")
    public LotAttrResponse updateLotAttr(
    		@RequestBody @Valid UpdateLotAttrRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
        	ValidatorUtil.validator(bindingResult);
            return lotAttrService.updateLotAttr(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    /**
     * 修改批次属性状态
     * @param dto
     * @param bindingResult
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "修改批次属性状态",notes = "修改批次属性状态")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PutMapping(value = REQUEST_MODEL + "/updateStatus")
    public <T> Result<T> updateStatus(
    		@RequestBody @Valid UpdateStatusLotAttrRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
        	ValidatorUtil.validator(bindingResult);
            return lotAttrService.updateStatus(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
}
